<?php

	// Register the request extension
	$n2f = n2f_cls::getInstance();
	$n2f->registerExtension(
		'n2f_request',
		'N2F Request',
		'0.1',
		'Chris Butcher',
		'http://www.cbutcher.org/'
	);

	define('N2F_TYPE_STRING',	'string');
	define('N2F_TYPE_INTEGER',	'integer');
	define('N2F_TYPE_DOUBLE',	'double');
	define('N2F_TYPE_BOOL',		'bool');
	define('N2F_TYPE_DATETIME',	'datetime');
	define('N2F_TYPE_JSON',		'json');
	define('N2F_TYPE_XML',		'xml');

	$n2f->loadExtension('n2f_request/classes');

	class n2f_request {

		/**
		 * Protected static property to hold singleton global.
		 *
		 * @var n2f_request
		 */
		protected static $_instance = null;


		/**
		 * Method to get the current n2f_request instance.
		 *
		 * @return n2f_request
		 */
		public static function &getInstance() {
			if(n2f_request::$_instance === null)
				n2f_request::$_instance = new n2f_request();

			$instance = n2f_request::$_instance;
			return $instance;
		}

		/**
		 * Check to see if the request object has a key with the supplied name.
		 *
		 * @param string $name
		 *
		 * @return bool
		 */
		public function exists($name) {
			if( empty($name) || strlen($name) < 1 )
				return false;

			return (!isset($_REQUEST[$name]) || empty($_REQUEST[$name]) ? false : true);
		}

		/**
		 * Set a new key/value pair in the request object.
		 * If the key already exists, it will only be rewritten if
		 * the rewrite value is set to true.
		 *
		 * @param string $key
		 * @param mixed $value
		 * @param bool $rewrite
		 *
		 * @return bool
		 */
		public function set($key, $value, $rewrite = true) {
			if( empty($key) || empty($value) )
				return false;

			if( $this->exists($key) )
				if( !is_bool($rewrite) || $rewrite_value )
					$_REQUEST[$key] = $value;
			else
				$_REQUEST[$key] = $value;

			return true;
		}

		/**
		 * Check to see if the request object has the key name,
		 * and if so then sanitize it.
		 *
		 * @param string $key
		 * @param string $type
		 * @param mixed $default
		 *
		 * @return bool|float|int|mixed|string
		 */
		public function get($key, $type = N2F_TYPE_STRING, $default = null) {
			$returnValue = null;

			if( $this->exists($key) )
				$returnValue = $_REQUEST[$key];

			else if( is_null($returnValue) )
				if( !is_null($default) )
					$returnValue = $default;

			if( !is_null($returnValue) ) {
				switch($type) {
					case N2F_TYPE_INTEGER:
						$returnValue = intval($returnValue);
						break;

					case N2F_TYPE_DOUBLE:
						$returnValue = floatval($returnValue);
						break;

					case N2F_TYPE_BOOL:
						$returnValue = (bool) $returnValue;
						break;

					case N2F_TYPE_DATETIME:
						if( is_string($returnValue) )
							if(!$returnValue = @new DateTime($returnValue))
								$returnValue = new DateTime();
						break;

					case N2F_TYPE_JSON:
						if( is_string($returnValue) )
							$returnValue = @json_decode($returnValue);
						break;

					case N2F_TYPE_XML:
						if( is_string($returnValue) )
							$returnValue = @simplexml_load_string($returnValue);
						break;

					default:
						$returnValue = (string) $returnValue;
						break;
				}
			}

			return $returnValue;
		}
	}
?>